<script setup lang="ts" name="DictionaryDrawer">
import { transformI18n } from '@/plugins/i18n'
import DictionaryEntryAddEdit from '../dictionaryEntryAddEdit/index.vue'
import useDictionaryEntry from '@/hooks/useDictionaryEntry'
const { getDictionaryEntryData } = useDictionaryEntry()
const props = defineProps({
  statusList: { type: Array as any }
})
const visible = ref(false)
const title = ref('')
const showDrawer = (v) => {
  console.log(v)
  visible.value = true
  title.value = v.name
  searchData.value.type = v.code
  getInitData()
}
// 子组件将方法暴露给父组件
defineExpose({ showDrawer })
const searchData:any = ref({
  label: '',
  type: '',
  status: 1
})
const dictionaryEntryData: any = ref([])
// 获取当前查询条件下的数据
const getInitData = async () => {
  const result: any = {}
  result.label = searchData.value.label
  result.status = searchData.value.status
  result.type = searchData.value.type
  dictionaryEntryData.value = await getDictionaryEntryData(result)
}
// 查询条件重置
const resetSearch = () => {
  searchData.value.label = ''
  searchData.value.status = 1
  getInitData()
}
const dictionaryEntryDialog = ref(null)
// 新增事件
const add = () => {
  dictionaryEntryDialog.value.showDictionaryEntryDialog(null)
}
// 编辑事件
const edit = (item) => {
  dictionaryEntryDialog.value.showDictionaryEntryDialog(item)
}

const deleteRow = (v) => {
  const msg = transformI18n('page.确定删除吗？', true)
  ElMessageBox.alert(msg, transformI18n('moduleTitle.警告', true), {
    showCancelButton: true,
    confirmButtonText: transformI18n('buttons.buttonConfirm', true),
    cancelButtonText: transformI18n('buttons.buttonCancel', true),
    type: 'warning',
    callback: (action) => {
      if (action === 'confirm') {
        ElMessage({
          type: 'info',
          message: `操作: 点击了确认`
        })
      } else {
        ElMessage({
          type: 'info',
          message: `操作: 点击了取消`
        })
      }
    }
  })
}
</script>

<template>
  <el-drawer class="dictionary-drawer" v-model="visible" direction="rtl" size="60%">
    <template #header>
      <h2>
        {{ $t('page.字典列表') }}
        <el-tag class="ml-10px">{{ title }}</el-tag>
      </h2>
    </template>
    <template #default>
      <el-form :inline="true">
        <el-form-item :label="$t('page.名称')">
          <el-input v-model="searchData.label" :placeholder="$t('placeholders.请输入') + $t('page.名称')" />
        </el-form-item>
        <el-form-item :label="$t('page.状态')">
          <el-radio-group class="radio-button" v-model="searchData.status">
            <el-radio-button v-for="(item, index) in statusList" :key="index" :label="item.value">{{ $t('page.' + item.label) }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="getInitData">{{ $t('buttons.buttonSearch') }}</el-button>
          <el-button icon="Refresh" @click="resetSearch">{{ $t('buttons.buttonReset') }}</el-button>
          <el-button type="success" icon="Plus" @click="add()">{{ $t('buttons.buttonCreate') }}</el-button>
        </el-form-item>
      </el-form>
      <div class="flex-1 table-layout">
        <div class="table-wrap">
          <el-table v-if="dictionaryEntryData.length > 0" :data="dictionaryEntryData" border :default-expand-all="false" row-key="id">
            <el-table-column prop="label" :label="$t('page.名称')" width="140" />
            <el-table-column prop="value" :label="$t('page.数据值')" width="140" />
            <el-table-column prop="status" :label="$t('page.状态')" width="90" align="center">
              <template #default="scope">
                <el-tag :type="scope.row.status ? 'success' : 'danger'">{{ scope.row.status ? $t('page.启用') : $t('page.停用') }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="sort" :label="$t('page.排序')" width="60" align="center" />
            <el-table-column prop="tagColor" :label="$t('page.标签颜色')" width="90" align="center">
              <template #default="scope">
                <el-tag v-if="scope.row.tagColor !== ''" :type="scope.row.tagColor">{{ scope.row.tagColor }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="icon" :label="$t('page.图标')" width="90" align="center">
              <template #default="scope">
                <svg-icon :icon="scope.row.icon" size="18"></svg-icon>
              </template>
            </el-table-column>
            <el-table-column prop="remark" :label="$t('page.备注')" />
            <el-table-column prop="operate" :label="$t('page.操作')" width="140" align="center">
              <template #default="scope">
                <el-button type="primary" icon="Edit" @click="edit(scope.row)"></el-button>
                <el-button type="danger" icon="Delete" @click="deleteRow(scope.row)"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <DictionaryEntryAddEdit ref="dictionaryEntryDialog" v-if="dictionaryEntryData.length > 0" :listData="dictionaryEntryData"></DictionaryEntryAddEdit>
    </template>
  </el-drawer>
</template>

<style lang="scss">
.dictionary-drawer {

  .el-drawer__header {
    margin: 0;
    padding: 15px var(--el-space);
    border-bottom: 1px solid var(--el-border-color-extra-light);
  }

  .el-drawer__footer {
    margin: 0;
    padding: 15px var(--el-space);
  }

  .el-drawer__body {
    display: flex;
    flex-direction: column;
    padding: calc(var(--el-space) - 6px) var(--el-space) var(--el-space);
  }

  .el-pagination {
    align-items: center;
    justify-content: flex-end;
  }
}

.table-layout {
  position: relative;
  margin: calc(var(--el-space) - 6px) 0 0;

  .table-wrap {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;

    .el-table {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
